package mat;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.util.Vector;

import javax.swing.JPanel;

import  types.MLabel;

class MLabelPanel extends JPanel{
	/**
	 * Panel drawer
	 */
	private static final long serialVersionUID = 1L;
	public MLabelPanel(){
		setOpaque(false);
	}
	public void pushNew(MLabel obj){
		ml.add(obj);		
	}
	
	public void setCoordiante(int index, int x, int y){
		ml.get(index).setXaxis(doubleToInt(x));
		ml.get(index).setYaxis(doubleToInt(y));
	}
	
	public void setString(int index, String str){
		ml.get(index).setContent(str);
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		for(int i=0; i< ml.size(); i++){
			xaxis = doubleToInt(ml.get(i).getXaxis());
			yaxis = doubleToInt(ml.get(i).getYaxis());
			text = ml.get(i).getContent();
			String[]MultLine = text.split("\n");
			String FontType = ml.get(i).getFontType();
			Font displayFont;
			if(FontType.equals("SansSerif")  ||
			   FontType.equals("Serif")      ||
			   FontType.equals("Monospaced") ||
			   FontType.equals("Dialog")     ||
			   FontType.equals("DialogInput")){
				if(ml.get(i).fontSize != 0){
					
					displayFont = new Font(FontType,Font.PLAIN,ml.get(i).fontSize);									}
				else{
					displayFont = new Font(FontType,Font.PLAIN,Mat.MyDoc.fontSize);
				}
				g.setFont(displayFont);
				}
				else{
				if(ml.get(i).fontSize != 0){
					displayFont = new Font(FontType,Font.PLAIN,ml.get(i).fontSize);									}
				else{
					displayFont = new Font(FontType,Font.PLAIN,Mat.MyDoc.fontSize);
				}
				g.setFont(displayFont);
			}

			FontRenderContext context = ((Graphics2D)g).getFontRenderContext();
			Rectangle2D bounds = displayFont.getStringBounds(text, context);
			double increheight = bounds.getHeight();
			double labelwidth = bounds.getWidth();
			
			int labelRightBorder = (int) (xaxis + labelwidth);
			int labelDownBorder = (int) (yaxis + increheight*MultLine.length);
			
			/*
			if(labelRightBorder > MCanvas.PhoneScreenWidth){
				xaxis = (int) (MCanvas.PhoneScreenWidth - labelwidth);
			}			
			if(labelDownBorder > MCanvas.PhoneScreenHeight){
				yaxis = (int) (MCanvas.PhoneScreenHeight - increheight*MultLine.length);
			}
			if(xaxis < 0){
				xaxis = 0;
			}			
			if(yaxis < 0){
				yaxis = 0;
			}
			*/
			
			for(int j = 0; j < MultLine.length; j++){
				//g.drawString(MultLine[j], (int) 0, 0);
				g.drawString(MultLine[j], (int) (xaxis - labelwidth/2), yaxis);
				yaxis = (int) (yaxis + increheight);
			}
			System.out.println("Label >> "+i+"==:"+xaxis+","+yaxis+","+text);
		}
	}
	
	private int doubleToInt(double d){
		Double t = new Double(d);
		return t.intValue();
	}
	private Vector<MLabel> ml = new Vector<MLabel>();
	private int xaxis;
	private int yaxis;
	private String text = "Hello World!!!";
}
